package com.org.cys.test.${package};

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.org.cys.pojo.base.Page;
import com.org.cys.service.impl.${package}.${entityName}ServiceImpl;
import com.org.cys.service.${package}.${entityName}Service;
import com.org.cys.util.IdWorker;
import com.org.cys.util.PageData;

/*
 * ${entityComment}单元测试
 * @author caiyushen
 */
@RunWith(SpringJUnit4ClassRunner.class)             //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class ${entityName}ServiceTest{
	
	private static Logger logger = Logger.getLogger(${entityName}ServiceTest.class);
	
	
	@Resource(name = "${entityNameSmall}ServiceImpl")
	private ${entityName}Service ${entityNameSmall}Service;
	
	
	/**
	 * 单元测试
	 * @throws Exception 
	 * */
	@Test
	public void test() throws Exception{
		PageData ${entityNameSmall} = save();//保存方法
		getEntityById((Long)${entityNameSmall}.get("id"));//加载实体类方法
		update(${entityNameSmall});//更新方法
		Page page = new Page();//分页列表
		page.setCurrentPage(1);//当前页
		page.setShowCount(20);//每页显示记录数
		dataList(page);//分页列表
		delete(${entityNameSmall});//删除方法
		listAll();//获取所有数据列表
		String[] idArray = saveBatch();//批量添加方法
		deleteAll(idArray);//批量删除方法

	}
	
	
	
	/**
	 * 保存${entityName}
	 * @param PageData
	 * @throws Exception
	 * */
	public PageData save() throws Exception{
		System.out.println("进入save方法");
		PageData pd = new PageData();
<#list dataList as var>		
	<#if var.dataType == 'String'>
		pd.put("${var.newColumn}","测试内容");				
	</#if>
	<#if var.dataType == 'Integer'>
		pd.put("${var.newColumn}","12");			
	</#if>
	<#if var.dataType == 'Long' && var.newColumn == 'id'>
		pd.put("id",IdWorker.getId());
	</#if>
	<#if var.dataType == 'Long' && var.newColumn != 'id'>
		pd.put("${var.newColumn}","1236547898745896");	
	</#if>
	<#if var.dataType == 'Date'>
		pd.put("${var.newColumn}",new Date());					
	</#if>
	<#if var.dataType == 'Float'>
		pd.put("${var.newColumn}","3.14159");					
 	</#if>
</#list>		
		${entityNameSmall}Service.save(pd);
		System.out.println("method-->save(), param--> pd: "+pd.toString());
		return pd;
	}
	
	/**
	 * 根据id获取${entityNameSmall}
	 * @throws Exception 
	 * */
	public void getEntityById(Long id) throws Exception{
		System.out.println("进入getEntityById方法");
		PageData pd = new PageData();
		pd.put("id", id);
		PageData ${entityNameSmall} = ${entityNameSmall}Service.getEntityById(pd);
		System.out.println("method-->getEntityById() entity-->"+ ${entityNameSmall}.toString());
	}
	
	
	/**
	 * 更新${entityName}
	 * @param PageData
	 * @throws Exception
	 * */
	public void update(PageData pd) throws Exception{
		System.out.println("进入update方法，入参：" + pd.toString());
<#list dataList as var>		
	<#if var.dataType == 'String'>
		pd.put("${var.newColumn}","测试修改内容");				
	</#if>
	<#if var.dataType == 'Integer'>
		pd.put("${var.newColumn}","99");			
	</#if>
	<#if var.dataType == 'Long'  && var.newColumn == 'id'>
		pd.put("${var.newColumn}",(Long)pd.get("id"));					
	</#if>
	<#if var.dataType == 'Long'  && var.newColumn != 'id'>
		pd.put("${var.newColumn}","9885956847616841");					
	</#if>
	<#if var.dataType == 'Date'>
		pd.put("${var.newColumn}",new Date());					
	</#if>
	<#if var.dataType == 'Float'>
		pd.put("${var.newColumn}","1.159857");					
 	</#if>
</#list>	
		${entityNameSmall}Service.update(pd);
		System.out.println("method-->update() entity-->"+ pd.toString());
	}
	
	/**
	 * ${entityName}列表
	 * @param PageData
	 * @throws Exception
	 * */
	 public List<PageData> dataList(Page page) throws Exception{
		 System.out.println("进入dataList方法");
		 List<PageData> dataList = ${entityNameSmall}Service.dataList(page);
		 System.out.println("method-->dataList() dataListSize: "+dataList.size());
		 return dataList;
	 }
	
	 /**
	  * 删除${entityName}
	  * @param PageData
	  * @throws Exception
	  * */
	 public void delete(PageData pd) throws Exception{
		 System.out.println("进入delete方法");
		 System.out.println("method-->delete() entity-->"+ pd.toString());
		 ${entityNameSmall}Service.delete(pd);	 
	 }
	 
	 
	 /**
	  * 获取所有${entityName}数据
	  * @param PageData
	  * @throws Exception
	  * */
	  public List<PageData> listAll() throws Exception{
		 System.out.println("进入listAll方法");
		 PageData pd = new PageData();
		 List<PageData> dataList = ${entityNameSmall}Service.listAll(pd);
		 System.out.println("method-->listAll() dataListSize: "+dataList.size());
		 return dataList;
	  }
	  
	 /**
	   * 批量添加${entityName}
	   * */
	  public String[] saveBatch() throws Exception{
		  System.out.println("进入saveBatch方法");
		  List<PageData> dataList = new ArrayList<>();
		  for(int i=0;i<10;i++){
			  PageData pd = new PageData();
	  <#list dataList as var>		
		  <#if var.dataType == 'String'>
			  pd.put("${var.newColumn}","测试内容");				
		  </#if>
		  <#if var.dataType == 'Integer'>
			  pd.put("${var.newColumn}","12");			
		  </#if>
		  <#if var.dataType == 'Long' && var.newColumn == 'id'>
			  pd.put("id",IdWorker.getId());
		  </#if>
		  <#if var.dataType == 'Long' && var.newColumn != 'id'>
			  pd.put("${var.newColumn}","1236547898745896");	
		  </#if>
		  <#if var.dataType == 'Date'>
			  pd.put("${var.newColumn}",new Date());					
		  </#if>
		  <#if var.dataType == 'Float'>
			  pd.put("${var.newColumn}","3.14159");					
	 	  </#if>
	</#list>
			  System.out.println(pd.toString());
			  dataList.add(pd);
		  }
		  ${entityNameSmall}Service.saveBatch(dataList);
		  int dataListSize = dataList.size();
		  String[] ArrayDATA_IDS = new String[dataListSize];
		  for(int i=0;i<dataListSize;i++){
			  ArrayDATA_IDS[i] = dataList.get(i).get("id").toString();
		  }
		  return ArrayDATA_IDS;
	  }
	  
	  
	  
	 
	  /**
	   * 批量删除${entityName}
	   * @param ArrayDATA_IDS[]
	   * @throws Exception
	   * */
	   public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		   System.out.println("进入deleteAll方法");
		   System.out.println("数组长度："+ArrayDATA_IDS.length);
		   ${entityNameSmall}Service.deleteAll(ArrayDATA_IDS);
		   listAll();
		   System.out.println("批量删除方法结束");
	   }
	 
	 
	
}